const { friendshipActivityDetail, friendshipActivityMemberList } = require('../../../../api/index');
const { formatTime2, countdownFn } = require('../../../../utils/util');

// pages/active/detail/index.js
Page({
  /*** 页面的初始数据 */
  data: {
    id: '',
    page: {
      pageNumber: 1,
      pageSize: 6,
      friendshipActivityId: ''
    },
    countdown: '',
    total: 0,
    detail: {},
    personList: [], //  报名人员列表
  },
  //  路径跳转
  onJump( event ) {
    const { url } = event.currentTarget.dataset;
    wx.navigateTo({ url });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    this.data.id = options.id;

    fetchFriendshipActivityDetail.call(this);
    fetchFriendshipActivityMemberList.call(this)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getPhoneNumber(e) { // 获取手机号码
    console.log(e);
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      this.phone_encryptedData = e.detail.encryptedData //用于解密
      this.phone_iv = e.detail.iv // 用于解密
      let that = this
      let pc = new WXBizDataCrypt(id, that.sessionKey);
      let data = pc.decryptData(that.phone_encryptedData, that.phone_iv);
      this.phoneNumber = data.phoneNumber;
      console.log(data);
      wx.setStorageSync("phoneNumber", this.phoneNumber);
      this.showEmpower = false
      this.showMessage = true
      let param = {
        phonenumber: wx.getStorageSync("phoneNumber"),
        openId: wx.getStorageSync("openid")
      }
      this.request('/visit/info/isStaff', param, 'POST').then(res => {
        if (res.code == 200) {
          if (res.data) {
            wx.setStorageSync("peopleData", 1);
            wx.setStorageSync("respondentId", res.data.id);
          } else {
            wx.setStorageSync("peopleData", 0);
          }
        } else {

        }
      })
    } else {
      console.log("用户点击了拒绝")
    }
  },
})

async function fetchFriendshipActivityDetail () {
  const { errors = [], friendshipActivity = {} } = await friendshipActivityDetail({ id: this.data.id });
  if(errors.length > 0) { return; } 
  this.setData({ detail: setResult.call(this, friendshipActivity ) });
}

async function fetchFriendshipActivityMemberList () {
  this.data.page.friendshipActivityId = this.data.id;
  const { errors = [], result = [], totalCount } = await friendshipActivityMemberList(this.data.page);
  this.setData({ total: totalCount });
  
  if(errors.length > 0) { return; } 
  this.setData({ personList: result });
}

function setResult(data) {
  const _data = JSON.parse(JSON.stringify(data));
  
  _data.applyTime = formatTime2(_data.applyTime);
  _data.activityBeginTime = formatTime2(_data.activityBeginTime);
  _data.activityEndTime = formatTime2(_data.activityEndTime);
  _data.registrationBeginTime = formatTime2(_data.registrationBeginTime);
  _data.registrationEndTime = formatTime2(_data.registrationEndTime);

  //  倒计时
  const t = setInterval(() => {
    const res = countdownFn( _data.activityEndTime );
    if(res === -1) {
      this.setData({ countdown: '活动已结束' });

      clearInterval(t);
      return;
    }
    this.setData({ countdown: countdownFn( _data.activityEndTime ) })
  }, 1000);
  return _data;
}